/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewAirlineDialog.java
 *
 * Created on 8.12.2011, 13:33:35
 */
package view;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import logic.business.DataEditor;
import logic.business.exceptions.MalformedDataException;

/**
 *
 * @author zhenia
 */
public class NewAirlineDialog extends javax.swing.JDialog {

    private DataEditor dataEditor;
    private boolean result;
    Locale[] locales = Locale.getAvailableLocales();
    private List<String> countriesCode = new ArrayList<String>();
    private String[] codes;

    /** Creates new form NewAirlineDialog */
    public NewAirlineDialog(DataEditor de) {
        dataEditor = de;
        initComponents();
        for (Locale locale : locales) {
            String code = locale.getDisplayCountry();
            if (!"".equals(code)) {
                countriesCode.add(code);
            }
        }
        codes = new String[countriesCode.size()];
        for (int i = 0; i < codes.length; i++) {
            codes[i] = countriesCode.get(i);
        }
        countries.setModel(new javax.swing.DefaultComboBoxModel(codes));
        countries.setSelectedIndex(1);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jComboBox1 = new javax.swing.JComboBox();
        cancel = new javax.swing.JButton();
        ok = new javax.swing.JButton();
        name = new javax.swing.JTextField();
        countrylabel = new javax.swing.JLabel();
        nameLabel = new javax.swing.JLabel();
        airlineCode = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        countries = new javax.swing.JComboBox();

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox1.setName("jComboBox1"); // NOI18N

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("New Airline");

        cancel.setText("Cancel");
        cancel.setName("cancel"); // NOI18N
        cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });

        ok.setText("OK");
        ok.setName("ok"); // NOI18N
        ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okActionPerformed(evt);
            }
        });

        name.setName("name"); // NOI18N
        name.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nameActionPerformed(evt);
            }
        });

        countrylabel.setText("Country");
        countrylabel.setName("countrylabel"); // NOI18N

        nameLabel.setText("Name");
        nameLabel.setName("nameLabel"); // NOI18N

        airlineCode.setName("airlineCode"); // NOI18N
        airlineCode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                airlineCodeActionPerformed(evt);
            }
        });

        jLabel1.setText("Code");
        jLabel1.setName("jLabel1"); // NOI18N

        countries.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        countries.setName("countries"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(nameLabel)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE)
                    .addComponent(countrylabel, javax.swing.GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(countries, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(name)
                    .addComponent(airlineCode, javax.swing.GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE))
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(ok, javax.swing.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE)
                    .addComponent(cancel, javax.swing.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameLabel)
                    .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ok))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(airlineCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cancel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(countrylabel)
                    .addComponent(countries, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(98, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed

        this.result = false;
        this.dispose();
}//GEN-LAST:event_cancelActionPerformed

    private void okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okActionPerformed
        try {
            if (airlineCode.getText().isEmpty() || name.getText().isEmpty()) {
                JOptionPane.showMessageDialog(this,
                        "Airline id and name can't be null",
                        "Warning",
                        JOptionPane.WARNING_MESSAGE);
            } else {
                dataEditor.addAirline(airlineCode.getText(), name.getText(), "0", (String) countries.getSelectedItem());
                this.result = true;
                this.dispose();
            }
        } catch (MalformedDataException ex) {
            JOptionPane.showMessageDialog(this,
                    "Airline id and name can't be null",
                    "Warning",
                    JOptionPane.WARNING_MESSAGE);
        }

}//GEN-LAST:event_okActionPerformed

    private void nameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nameActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_nameActionPerformed

private void airlineCodeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_airlineCodeActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_airlineCodeActionPerformed

    boolean execute() {
        this.setVisible(true); // прорисовать дочернее окно; в данном месте выполнение программы приостановится, ожидая окончания работы пользователя (в нашем случае - функции dispose())
        return this.result;
    }
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField airlineCode;
    private javax.swing.JButton cancel;
    private javax.swing.JComboBox countries;
    private javax.swing.JLabel countrylabel;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField name;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JButton ok;
    // End of variables declaration//GEN-END:variables
}
